$(document).ready(function() {
  require(["inputdrop", "asset"], function(inputdrop, asset) {

    index_title_set("终端详情")

    $("#content_div").css("padding-left" ,"20px");

    var software_list_col = [
      { name: "", text: "", hideSearch: "hide", width: 3 },
      { text: "序号", name: "operator", align: "left", width: 15 },
      {
        text: "应用名称",
        name: "logType",
        align: "left",
        width: 15,
        searchRender: function(el) {
          el.append(
            '<input type="text" class="form-control input-sm" search-data="userName">'
          );
        }
      },
      { text: "包名", name: "message", align: "left", width: 10 },
      { text: "应用版本", name: "message", align: "left", width: 10 },
      { text: "应用大小", name: "message", align: "left", width: 10 },
      { text: "系统应用", name: "message", align: "left", width: 10 },
      {
        text: "安装时间",
        name: "time",
        align: "left",
        width: 15,
        render: function(text) {
          return text ? new Date(text).Format("yyyy-MM-dd hh:mm:ss") : "";
        }
      },
      {
        text: "上报时间",
        name: "time",
        align: "left",
        width: 15,
        render: function(text) {
          return text ? new Date(text).Format("yyyy-MM-dd hh:mm:ss") : "";
        }
      }
    ];

    var instruction_list_col =  [
      { name: "", text: "", hideSearch: "hide", width: 3 },
      { text: "序号", name: "operator", align: "left", width: 15 },
      {
        text: "指令名称",
        name: "logType",
        align: "left",
        width: 15,
        searchRender: function(el) {
          el.append(
            '<input type="text" class="form-control input-sm" search-data="userName">'
          );
        }
      },
      { text: "下发方式", name: "message", align: "left", width: 10 },
      { text: "执行状态", name: "message", align: "left", width: 10 },
      { text: "部署人员", name: "message", align: "left", width: 10 },
      {
        text: "下发时间",
        name: "time",
        align: "left",
        width: 15,
        render: function(text) {
          return text ? new Date(text).Format("yyyy-MM-dd hh:mm:ss") : "";
        }
      },
      {
        text: "执行时间",
        name: "time",
        align: "left",
        width: 15,
        render: function(text) {
          return text ? new Date(text).Format("yyyy-MM-dd hh:mm:ss") : "";
        }
      }
    ];

    var event_list_col =  [
      { name: "", text: "", hideSearch: "hide", width: 3 },
      { text: "序号", name: "operator", align: "left", width: 15 },
      {
        text: "时间名称",
        name: "logType",
        align: "left",
        width: 15,
        searchRender: function(el) {
          el.append(
            '<input type="text" class="form-control input-sm" search-data="userName">'
          );
        }
      },
      { text: "时间类型", name: "message", align: "left", width: 10 },
      { text: "时间等级", name: "message", align: "left", width: 10 },
      {
        text: "发生时间",
        name: "time",
        align: "left",
        width: 15,
        render: function(text) {
          return text ? new Date(text).Format("yyyy-MM-dd hh:mm:ss") : "";
        }
      }
    ];
   
    var software_list_url = "api/public/syslog/pmmc";
    var instruction_list_url = "api/public/syslog/pmmc";
    var event_list_url = "api/public/syslog/pmmc";

    var software_list_element = "#software_table";
    var instruction_list_element = "#instruction_table";
    var event_list_element = "#terminal_event";

    var list_url;
    var bing_element;
    var list_header;

    $("#terminal_tab li").unbind('click').click(function() {
        // 导航的蓝线
        $(this).addClass('active');
        $(this).siblings().removeClass('active');
       
        // 导航的图标
        var i_icon = $("#terminal_tab li i");
        for(var i=0;i<i_icon.length;i++){
          $(i_icon[i]).removeClass('active');
        }
        var now_i_icon = $(this).children()[0];
        $(now_i_icon).addClass('active');

        // 选项卡内容
        var tab_panel_arr = $(this).parent().siblings('.tab-content').children();
        var tab_nav_index = $(this).index();
        var select_now_panel =  tab_panel_arr[tab_nav_index];
        $(select_now_panel).addClass('active');
        $(select_now_panel).siblings().removeClass('active');

        
        // 应用软件
        if( $(select_now_panel).attr("id") == "terminal_software"){
          bing_element = software_list_element;
          list_header = software_list_col;
          list_url = software_list_url;

          terminal_list({ paramObj: null, isLoad: true, maskObj: "body" });
        }
        // 指令记录
        else if( $(select_now_panel).attr("id") == "terminal_instruction" ){
          bing_element = instruction_list_element;
          list_header = instruction_list_col;
          list_url = instruction_list_url;

          // 交互效果逻辑代码
          $("input[placeholder]").placeholder();
          laydate.render({
            elem: "#timeRange",
            range: true,
            max: 0,
            done: function(value, date, endDate) {
              console.log(value); //得到日期生成的值，如：2017-08-18
              var startTime = "",
                endTime = "";
              if (value) {
                var time = value.split(" - ");
                startTime = Date.parse(new Date(time[0]));
                endTime = Date.parse(new Date(time[1]));
              }
              g_grid.refresh($(bing_element), {
                paramObj: {
                  startTime: startTime,
                  endTime: endTime
                }
              });
            }
          });
          

          terminal_list({ paramObj: null, isLoad: true, maskObj: "body" });
        }
        // 事件记录
        else if( $(select_now_panel).attr("id") == "terminal_event" ){
          bing_element = event_list_element;
          list_header = event_list_col;
          list_url = event_list_url;
          terminal_list({ paramObj: null, isLoad: true, maskObj: "body" });
        };

       
    });



    function terminal_list(option) {
      g_grid.render($(bing_element), {
        header: list_header,
        url: list_url,
        type: "xd",
        dataKey: "list",
        hasBorder: false,
        hideSearch: true,
        paramObj: option.paramObj,
        isLoad: option.isLoad,
        maskObj: option.maskObj,
        autoHeight: true,
        showCount: true,
        allowCheckBox: false
      });
    }
   
  });
});


